package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.mapper.BasicCustomerMapper;
import cn.stylefeng.guns.modular.basic.model.params.BasicCustomerParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCustomerResult;
import cn.stylefeng.guns.modular.basic.model.result.JHZYJDResult;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * 客户表 服务实现类
 */
@Service
public class BasicCustomerServiceImpl extends ServiceImpl<BasicCustomerMapper, BasicCustomer> implements BasicCustomerService {
    
    @Resource
    private DeptService deptService;

    @Override
    public void add(BasicCustomerParam param){
        BasicCustomer entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BasicCustomerParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BasicCustomerParam param){
        BasicCustomer oldEntity = getOldEntity(param);
        BasicCustomer newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BasicCustomerResult findBySpec(BasicCustomerParam param,Long operateId){
        return this.baseMapper.findBySpec(param,operateId);
    }

    @Override
    public List<BasicCustomerResult> findListBySpec(BasicCustomerParam param,Long operateId){
        return this.baseMapper.findListBySpec(param, operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(BasicCustomerParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param, condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BasicCustomerParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BasicCustomer getOldEntity(BasicCustomerParam param) {
        return this.getById(getKey(param));
    }

    private BasicCustomer getEntity(BasicCustomerParam param) {
        BasicCustomer entity = new BasicCustomer();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public List<BasicCustomer> findListByDeptId(Long deptId) {
		return this.baseMapper.findListByDeptId(deptId);
	}

    @Override
    public BasicCustomer findListByStoreIdXlb(String storeIdXlb) {
        return this.baseMapper.findListByStoreIdXlb(storeIdXlb);
    }

    @Override
    public String findListByWarehouseId(String warehouseId) {
        return this.baseMapper.findListByWarehouseId(warehouseId);
    }

    @Override
	public List<BasicCustomerResult> findAvailableList(Long deptId, Long deliverId,Long operateId) {
		BasicCustomerParam param = new BasicCustomerParam();
		param.setDeptId(deptId);
		param.setDeliverId(deliverId);
		return this.findListBySpec(param,operateId);
	}

	@Override
	public List<BasicCustomerResult> findpullStoragetaskXlbListBySpec(BasicCustomerParam param) {
		return this.baseMapper.findpullStoragetaskXlbListBySpec(param);
	}
    @Override
    public List<BasicCustomerResult> findpullYuyueXlbListBySpec(BasicCustomerParam param) {
        return this.baseMapper.findpullYuyueXlbListBySpec(param);
    }
    @Override
    public List<BasicCustomerResult> findpullReturnXlbListBySpec(BasicCustomerParam param) {
        return this.baseMapper.findpullReturnXlbListBySpec(param);
    }
    @Override
    public List<BasicCustomerResult> findpullReturnXlbgoodsListBySpec(BasicCustomerParam param) {
        return this.baseMapper.findpullReturnXlbgoodsListBySpec(param);
    }

    @Override
    public List<JHZYJDResult> findJhzyjdListByDeptId(Long deptId, String currentDate) {
        return this.baseMapper.findJhzyjdListByDeptId(deptId, currentDate);
    }
}
